package org.example.petitionplatformsystem.controller;

import org.example.petitionplatformsystem.Response;
import org.example.petitionplatformsystem.dao.model.News;
import org.example.petitionplatformsystem.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RequestMapping("admin_panel/categroy")
@RestController
public class newsController {
    //新闻表的服务
    private final NewsService newsService;
    @Autowired
    public newsController(NewsService newsService){
        this.newsService = newsService;
    }

    @GetMapping("/{categroyid}/news")
    public Response<List<News>> getNews(@PathVariable long categroyid){
        return Response.newSuccessResponse(newsService.getCategoriesByCategoryID(categroyid));
    }
    @PostMapping("/{categroyid}/new")
    public Response<Void> createNews(@PathVariable long categroyid,@RequestParam String newsTitle,@RequestParam String newsContent){
        LocalDateTime currentTime = LocalDateTime.now();
        News news = new News(categroyid, newsTitle, newsContent,currentTime);


        newsService.addNews(news);

        return Response.newSuccessResponse(null);
    }
    @DeleteMapping("/{categroyid}/new/{newsid}")
    public Response<Void> deleteNews(@PathVariable long categroyid,@PathVariable long newsid){
        newsService.deleteNews(newsid);

        return Response.newSuccessResponse(null);
    }
    @PutMapping("/{categroyid}/new/{newsid}")
    public Response<Void> updateNews(@PathVariable long categroyid,@PathVariable long newsid,@RequestParam String newsTitle,@RequestParam String newsContent){
        News news = newsService.getNewsById(newsid);
        news.setNewsTitle(newsTitle);
        news.setNewsContent(newsContent);

        newsService.updateNews(news);
        return Response.newSuccessResponse(null);
    }
}
